<template>
    <div class="wrapper">
        <header>
            发现更多好吃的
        </header>
        <div class="search">
            <div class="search-fixed-top" ref="fixedBox">
                <div class="search-box">
                    <i class="fa fa-search"></i>搜索饿了么商家，商品名称
                </div>
            </div>
        </div>
        <ul class="business">
            <li v-for="item in businessArr" @click="toBusinessInfo(item.businessId)">
                <div class="business-img">
                    <img :src="item.businessImg">
                    <div class="business-img-quantity" v-show="item.quantity > 0">{{ item.quantity }}</div>
                </div>
                <div class="business-info">
                    <h3>{{ item.businessName }}</h3>
                    <p>&#165;{{ item.starPrice }}起送 | &#165;{{ item.deliveryPrice }}配送</p>
                    <p>{{ item.businessExplain }}</p>
                </div>
            </li>
        </ul>

        <Footer></Footer>
    </div>
</template>

<script>

import Footer from '../components/Footer.vue'

export default {
    name: 'Discover',
    data() {
        return {
            orderTypeId: this.$route.query.orderTypeId,
            businessArr: [],
            user: {}
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
        this.$axios.get('BusinessController/listBusiness', {
            params: {
                orderTypeId: this.orderTypeId
            }
        }).then(response => {
            this.businessArr = response.data.data;
            if (this.user != null) {
                this.listCart();
            }
        }).catch(error => {
            console.log('listBusinessByOrderTypeId请求失败');
            console.log(error);
        });
    },
    beforeMount() {
        document.onscroll = () => {
            //获取滚动条位置
            let s1 = document.documentElement.scrollTop;
            let s2 = document.body.scrollTop;
            let scroll = s1 == 0 ? s2 : s1;
            //获取视口宽度
            let width = document.documentElement.clientWidth;
            //获取顶部固定块
            let search = this.$refs.fixedBox;
            console.log(search);
            //判断滚动条超过视口宽度的12%时，搜索块变固定定位
            if (scroll > width * 0.12) {
                search.style.position = 'fixed';
                search.style.left = '0';
                search.style.top = '0';
            } else {
                search.style.position = 'static';
            }
        }
    },
    //vue3改为beforeUnmount
    beforeUnmount() {

        console.log("移除组件");
        //切换组件时移除滚动条事件
        document.onscroll = null;
    },
    components: {
        Footer
    },
    methods: {
        listCart() {
            this.$axios.get('CartController/listCart', {
                params: {
                    userId: this.user.userId
                }
            }).then(response => {
                let cartArr = response.data.data;
                for (let businessItem of this.businessArr) {
                    businessItem.quantity = 0;
                    for (let cartItem of cartArr) {
                        if (cartItem.businessId == businessItem.businessId) {
                            businessItem.quantity += cartItem.quantity;
                        }
                    }
                }
                this.businessArr.sort();
            }).catch(error => {
                console.error(error);
            })
        },
        toBusinessInfo(businessId) {
            this.$router.push({ path: '/businessInfo', query: { businessId: businessId } });
        }
    }
}

</script>

<style scoped>
/*              wrapper              */
.wrapper {
    width: 100%;
    height: 100%;
}

.wrapper header {
    width: 100%;
    height: 6vw;
    background-color: #0097FF;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 4.2vw;
    color: white;
    /* flex-direction: column; */
}

/************* search **************/
.wrapper .search {
    width: 100%;
    height: 13vw;
    display: flex;
}

.wrapper .search .search-fixed-top {
    width: 100%;
    height: 13vw;
    background-color: #0097FF;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1;
}

.wrapper .search .search-fixed-top .search-box {
    width: 90%;
    height: 9vw;
    background-color: #fff;
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 3.5vw;
    color: #AEAEAE;
    font-family: "宋体";
    /*此样式是让文本选中状态无效*/
    user-select: none;
}

.wrapper .search .search-fixed-top .search-box .fa-search {
    margin-right: 1vw;
}

.wrapper .business {
    width: 100%;
    margin-top: 2vw;
    margin-bottom: 14vw;
    z-index: 1;
}

/* 商家 */
.wrapper .business li {
    width: 100%;
    box-sizing: border-box;
    padding: 2.5vw;
    border-bottom: solid 1px #DDD;
    user-select: none;
    cursor: pointer;
    display: flex;
    align-items: center;
}

.wrapper .business li .business-img {
    /*这里设置为相当定位，成为business-img-quantity元素的父元素*/
    position: relative;
}

.wrapper .business li .business-img img {
    width: 20vw;
    height: 20vw;
}

.wrapper .business li .business-img .business-img-quantity {
    width: 5vw;
    height: 5vw;
    background-color: red;
    color: #fff;
    font-size: 3.6vw;
    border-radius: 2.5vw;
    display: flex;
    justify-content: center;
    align-items: center;
    /*设置成绝对定位，不占文档流空间*/
    position: absolute;
    right: -1.5vw;
    top: -1.5vw;
}

.wrapper .business li .business-info {
    margin-left: 3vw;
}

.wrapper .business li .business-info h3 {
    font-size: 3.8vw;
    color: #555;
}

.wrapper .business li .business-info p {
    font-size: 3vw;
    color: #888;
    margin-top: 2vw;
}
</style>